package com.dg.lecheng.api.user.service;

import com.dg.lecheng.api.user.model.InfoUser;
import com.dg.lecheng.api.user.repo.InfoUserDao;
import com.dg.lecheng.base.core.service.CrudService;
import com.dg.lecheng.base.core.utils.MD5Util;
import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class InfoUserService extends CrudService<InfoUserDao, InfoUser, Integer> {
    @Autowired
    private InfoUserRoleService infoUserRoleService;
    @Autowired
    private InfoUserAuthService infoUserAuthService;

    public List<InfoUser> findByPhoneNumAndEmail(String phoneNum, String email, PageBounds pageBounds){
        return curdRepo.findByPhoneNumAndEmail(phoneNum,email,pageBounds);
    }

    public InfoUser getByUserName(String userName){
        return curdRepo.getByUserName(userName);
    }

    public void updatePw(String newPw){
        InfoUser infoUser = (InfoUser) SecurityUtils.getSubject().getSession().getAttribute("infoUser");
        InfoUser newUser = new InfoUser();
        newUser.setId(infoUser.getId());
        newUser.setPassword(MD5Util.MD5Encode(newPw));
        curdRepo.update(newUser);
    }

    public void removeUserAndRelatedInfo(int userId){
        curdRepo.remove(userId);
        /**
         * 删除用户对应的角色 用户对应的资源 用户对应的公司
         */
        infoUserRoleService.removeByUserId(userId);
        infoUserAuthService.removeByUserId(userId);
        infoUserAuthService.removeByUserId(userId);
    }

}